package wnhz.com.business.uitls.pickerDialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import java.util.List;

import wnhz.com.business.R;
import wnhz.com.business.uitls.pickerDialog.adapter.ListWheelAdapter;

/**
 * Created by diycoder on 16/6/23.
 */
public class PickerPopWindow extends PopupWindow implements OnWheelChangedListener {
    static final String TAG = "PickerPopWindow";

    public List<String> mDatas;
    public String mCurrentName;

    public String DEFAULT_PICKVALUE = "请选择";
    public String pickValue = DEFAULT_PICKVALUE;

    public Context mContext;
    public WheelView mView;
    public Button closePopWindow;
    public Button showValue;

    public final View pickerView;
    public LayoutInflater inflater;
    public Display display;
    public WindowManager windowManager;

    public PickListener pickListner;

    public PickerPopWindow(Activity context, List<String> mDatas) {
        super(context);
        this.mContext = context;
        this.mDatas = mDatas;

        //初始化数据
        initProvinceDatas();

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        pickerView = inflater.inflate(R.layout.item_popwindow, null);
        closePopWindow = (Button) pickerView.findViewById(R.id.btn_closePop);
        showValue = (Button) pickerView.findViewById(R.id.btn_showValue);
        mView = (WheelView) pickerView.findViewById(R.id.id_wheel);

        //设置PickerPopWindow的View
        this.setContentView(pickerView);
        //设置PickerPopWindow弹出窗体的宽

        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        this.setWidth(RelativeLayout.LayoutParams.FILL_PARENT);
        //设置PickerPopWindow弹出窗体的高
        this.setHeight((int) (display.getHeight() * 0.40));
        //设置PickerPopWindow弹出窗体可点击
        this.setFocusable(true);
        //设置PickerPopWindow可触摸
        this.setTouchable(true);
        //设置PickerPopWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x90000000);
        //设置PickerPopWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        setUpListener();
        setUpData();
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
        } else {
            this.dismiss();
        }
    }

    /**
     * 设置选择器监听器
     */
    public void setUpListener() {
        mView.addChangingListener(this);

        showValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pickListner != null) {
                    pickListner.pickValue(pickValue);
                }
                dismiss();
            }
        });
        closePopWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 初始化选择器数据
     */
    public void setUpData() {
        ListWheelAdapter<String> stringArrayWheelAdapter = new ListWheelAdapter<>(mContext, mDatas);
        mView.setViewAdapter(stringArrayWheelAdapter);
        mView.setCurrentItem(0);
        mView.setVisibleItems(6);
    }

    /**
     * 初始化数据
     */
    public void initProvinceDatas() {
        mCurrentName = mDatas.get(0);
    }

    /**
     * 滚动监听
     *
     * @param wheel    the wheel view whose state has changed
     * @param oldValue the old value of current item
     * @param newValue the new value of current item
     */

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mView) {
            mCurrentName = mDatas.get(newValue);
        }else{
            mCurrentName = mDatas.get(oldValue);
        }
        pickValue = mCurrentName;
    }

    public interface PickListener {
        void pickValue(String value);
    }

    public void setPickListener(PickListener listener) {
        this.pickListner = listener;
    }
}
